﻿using System.Text.RegularExpressions;
using ValueOf;

namespace Customers.Api.Domain.Common;

public class Email : ValueOf<string, Email>
{
    private static readonly Regex EmailRegex = new("^[\\w!#$%&’*+/=?`{|}~^-]+(?:\\.[\\w!#$%&’*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$",
        RegexOptions.Compiled | RegexOptions.IgnoreCase);

    protected override void Validate()
    {
        if (!EmailRegex.IsMatch(Value))
        {
            this.ThrowValidationException(nameof(Email), $"{Value} is not a valid email address");
        }
    }
}